/**
 * 银行账号脱敏
 * @param acount
 * @returns {string}
 */
export const hideBankAcount = (acount) => {
	const length = acount.length;
	if (!acount || length <= 8) {
		return acount;
	}
	// 取出前四位
	const firstTwo = acount.substring(0, 4);
	// 后四位
	const lastFour = acount.substring(length - 4);

	const middel = "*".repeat(length - 8);
	return firstTwo + middel + lastFour;

	// let newStr = '';
	// const stars = '****';
	// const {
	// 	length
	// } = acount;
	// if (length % 4 === 0) {
	// 	const times = length / 4 - 1;
	// 	for (let i = 1; i < times; i++) {
	// 		newStr += acount.substr(0, 4);
	// 		newStr += stars;
	// 	}
	// 	newStr += acount.substr(-4, 4);
	// } else {
	// 	const tailLength = length % 4;
	// 	const times = Math.trunc(length / 4);
	// 	debugger;
	// 	for (let i = 0; i < times; i++) {
	// 		newStr += stars;
	// 	}
	// 	newStr += acount.substr(-tailLength, tailLength);
	// }
	// return newStr;
};

/**
 * 格式化银行账号  账四位一组显示
 */
export const formateBankAcount = (str) => {
	if (!str) {
		return '';
	}
	return str.replace(/(\S{4})/g, '$1 ');
}

/** *
 * 身份证脱敏
 * @param value (string)
 */
export const hideIdCard = (value) => {
	if (!value) return '';
	return value.replace(/^(.{6})(?:\d+)(.{4})$/, '$1********$2');
};

/** *
 * 校验手机号
 * @param value
 * @returns {boolean}
 */
export const isMobilePhone = (value) => {
	const reg = /^1[3-9]\d{9}$/;
	return reg.test(value);
};

/**
 * 手机号分为344格式；如： 123 456 7890
 */
export const spliltPhone = (value) => {
	if (!isMobilePhone(value)) {
		return '';
	}

	return value.replace(/\s/g, '').replace(/(^\d{3})(?=\d)/g, "$1 ").replace(/(\d{4})(?=\d)/g, "$1 ");
};

// 至少包含大写字母，小写字母，数字，特殊字符
export const passwordVal = (value) => {
	const reg = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,16}$/;
	return reg.test(value);
};

export const passwordVal1 = (value) => {
	// const reg = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,16}$/;

	const reg = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()._=[\]{}|;:,<>\?`~\-])(?!.*[<>'"/+]).{8,16}$/
	console.log(reg.test(value));
	return reg.test(value);
};

/** *
 * 手机号脱敏
 * @param value (string)
 */
export const hidePhoneNumber = (value) => {
	if (!value) {
		return ''
	}
	if (!value) return '';
	const regex = /^(\d{3})\d{4}(\d{4})/;
	const matches = value.match(regex);
	if (matches) {
		return `${matches[1]}****${matches[2]}`;
	}
	return value;
};

/**
 * 姓名脱敏
 */
export const desensitizeName = (name) => {
	if (!name) return '';
	let arr = Array.from(name);
	if (arr.length === 2) {
		return arr[0] + '*';
	} else
	if (arr.length > 2) {
		for (let i = 1; i < arr.length; i++) {
			if (i < arr.length - 1) {
				arr[i] = '*';
			}
		}
		return arr.join('');
	}
	return name;
}

/** *
 * 根据key分组， 返回二维数据
 * @param arr
 * @param key
 * @returns {*[]|*}
 */
// export const getGroupDataByKey = (arr, key) => {
//   if (!key) return arr;
//   const aMap = [];
//   const aResult = [];
//   for (let i = 0; i < arr.length; i++) {
//     const obj = {
//       jkqx: '',
//       list: [],
//     };
//     const item = arr[i];
//     if (aMap.indexOf(item[key]) < 0) {
//       obj.jkqx = item[key];
//       obj.list.push(item);
//       aResult.push(obj);
//       aMap.push(item[key]);
//     } else {
//       const index = aMap.indexOf(item[key]);
//       aResult[index].list.push(item);
//     }
//   }
//   return aResult;
// };

export const getGroupDataByKey = (arr, key) => {
	if (!key) return arr;
	const aMap = [];
	const aResult = [];
	for (let i = 0; i < arr.length; i++) {
		const item = arr[i];
		if (aMap.indexOf(item[key]) < 0) {
			aResult.push([item]);
			aMap.push(item[key]);
		} else {
			const index = aMap.indexOf(item[key]);
			aResult[index].push(item);
		}
	}
	return aResult;
};

/** *
 * 数组去重
 * @param data
 * @param key 去重字段
 */
export const removeDoubleData = (data, key) => {
	const tempArr = [];
	const obj = {};
	data.map((item) => {
		if (!obj[item[key]]) {
			tempArr.push(item);
			obj[item[key]] = true;
		}
		return item;
	});
	return tempArr;
};

function checkBirthday(idCard) {
	const year = idCard.substring(6, 10);
	const month = idCard.substring(10, 12);
	const day = idCard.substring(12, 14);
	const tempDate = new Date(year, parseFloat(month) - 1, parseFloat(day));
	const age = new Date().getFullYear() - parseFloat(year); // 这里用getFullYear()获取年份，避免千年虫问题

	return (
		tempDate.getFullYear() === parseFloat(year) &&
		tempDate.getMonth() === parseFloat(month) - 1 &&
		tempDate.getDate() === parseFloat(day) &&
		age >= 0 &&
		age <= 150
	);
}

/**
 * 最小字符长度校验
 */
export const minCharacterLenRule = (len) => {
	return {
		min: len,
		message: `长度不应小于 ${len} 字符`
	};
};

/**
 * 固定长度校验
 */
export const fixedLenRule = (len) => {
	return {
		len,
		message: `长度应小于等于 ${len} 字符`
	};
};

const validateMaxLength = (val, len) => {
	if (val.length > len) {
		return {
			result: false,
			message: `长度应小于等于 ${len} `,
			type: 'error'
		};
	}
	return {
		result: true
	};
};

const validateMixLength = (val, len) => {
	if (val.length < len) {
		return {
			result: false,
			message: `长度不应小于 ${len} `,
			type: 'error'
		};
	}
	return {
		result: true
	};
};

/**
 * 校验必须数字 不包含小数
 */
export const numberIntRule = {
	pattern: /^\d*$/,
	message: '必须为数字'
};

/**
 * 校验必须数字 包含小数
 */
export const numberRule = {
	pattern: /^\d+(\.\d+)?$/,
	message: '请输入正确的数字'
};

/**
 * 最大长度校验
 */
export const maxLenRule = (len) => {
	return {
		validator: (val) => validateMaxLength(val || '', len)
	};
};

/**
 * 最小长度校验
 */
export const minLenRule = (len) => {
	return {
		validator: (val) => validateMixLength(val || '', len)
	};
};

/**
 * 字符长度校验
 */
export const maxCharacterLenRule = (len) => {
	return {
		max: len,
		message: `长度应小于等于 ${len} 字符`
	};
};

/**
 * 校验数字横杆组合
 */
export const numberJointMarkRule = {
	pattern: /^[\d-]*?$/,
	message: '只能数字与横杠符合的任意组合'
};
/* 身份证脱敏 */
export const deseIdCard = (value) => {
	return value.replace(/^(.{6})(?:\d+)(.{4})$/, '$1********$2');
};
/**
 * 校验输入表情
 */
export const includeSpecial = {
	pattern:
		// /^[\uD83c|\uD83D|\uD83E][\uDC00-\uDFFF][\u200D|uFE0F][\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF]|[0-9|*|#]\uFE0F\u20E3|[0-9|#]\u20E3|[\u203C-\u3299]\uFE0F\u200D|[\u203C-\u3299]\uFE0F|[\u2122-\u2B55]|\u303D|[A9|AE]\u3030\u3030+$/gi,
		/[a-zA-Z0-9_|,.\\'"~!@#$%^&*()\-+=<>?:;【】{}《》\\[\u4e00-\u9fa5]/,
	message: '不能输入特殊字符',
};
/**
 * 身份证校验
 */
export function isIDCard(value) {
	const isIDCardReg =
		/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X|x)$/; // 校验位

	if (typeof value !== 'string') return false;
	const idCard = value.trim();
	return idCard.length === 18 && isIDCardReg.test(idCard) && checkValidCode(idCard) && checkBirthday(idCard);
}
/* 军官、护照脱敏 */
export const deseCard = (value) => {
	return value.replace(/^(.{3})(?:\d+)(.{3})$/, '$1***$2');
};

function checkValidCode(idCard) {
	const newIdCard = idCard.split('');
	const wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1];
	const valideCode = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2]; // 加权因子
	let sum = 0; // 声明加权求和变量

	if (newIdCard[17].toLowerCase() === 'x') {
		newIdCard[17] = 10; // 将最后位为x的验证码替换为10方便后续操作
	}

	for (let i = 0; i < 17; i++) {
		sum += wi[i] * newIdCard[i]; // 加权求和
	}

	const valCodePosition = sum % 11; // 得到验证码所位置

	return +newIdCard[17] === valideCode[valCodePosition];
}
export function transFormCard(str, forntLen = 6, endLen = 4) {
	if (!str || str < 10) {
		return str;
	}
	const len = str.length - forntLen - endLen;
	let start = '';
	for (let i = 0; i < len; i++) {
		start += '*';
	}
	return `${str.substring(0, forntLen)}${start}${str.substring(str.length - endLen)}`;
}
export function valitPhone(val) {
	const reg01 = /^1[3456789]\d{9}$/; // 手机

	if (val) {
		if (val.substr(0, 1) !== '0') {
			return reg01.test(val);
		}
	}
	return false;
}